<?php

namespace exphp;

class OutPut
{

    private static $format;
    private static $charset;
    private static $apis;

    /**
     * 初始化System配置的详细信息
     * @return void
     */
    private static function initSystemConfig()
    {
        $configPath = PathOperations::getExConfigFilePath('System.php');
        is_file($configPath) and $systemConfig = require(PathOperations::getExConfigFilePath('System.php'));
        self::$apis = $systemConfig['apis'] ?? '';
        self::$format = $systemConfig['format'] ?? '';
        self::$charset = $systemConfig['charset'] ?? '';
    }

    private static function setHttpStatus($status)
    {
        header("HTTP/1.1 {$status}");
        self::initSystemConfig();
        if (self::$format == 'json' || self::$format == 'xml') {
            header("Content-type: application/" . self::$format . "; charset=" . self::$charset);
        }
    }

    private static function restfulSuccessOutput($data)
    {
        if (self::$format == 'json') {
            exit(DataTypeConvert::convertToJson($data));
        } elseif (self::$format == 'xml') {
            exit(DataTypeConvert::convertToXml($data));
        } else {
            exit(DataTypeConvert::convertToString($data));
        }
    }

    private static function restfulErrorOutput($code = '', $message = '', $status = 400, $errorUrl = '')
    {
        $data = array('code' => $code, 'msg' => $message, 'errorUrl' => $errorUrl);
        if (self::$format == 'json') {
            exit(DataTypeConvert::convertToJson($data));
        } elseif (self::$format == 'xml') {
            exit(DataTypeConvert::convertToXml($data));
        } else {
            exit($message);
        }
    }

    /** 
     * 处理成功
     * @param  any $data 需要返回的数据
     * @return any 返回json格式数据
     */
    public static function successMessage($data = '')
    {
        self::setHttpStatus(200);
        if (self::$apis == 'restful') {
            self::restfulSuccessOutput($data);
        }
    }

    /** 
     * 异常处理
     * @param string $code 错误码
     * @param string $state 需要返回的状态码
     * @return json 返回json格式数据
     */
    public static function errorMessage($code = '', $message = '', $status = 400)
    {
        self::setHttpStatus($status);
        $debugArrayInfo = debug_backtrace();
        $debugId = CharProcess::createUuid();
        $dir = PathOperations::getProjectPath('/runtime/debug/' . date('Ymd', time()));
        if (!is_dir($dir)) {
            mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
        }
        file_put_contents($dir . DIRECTORY_SEPARATOR . $debugId, DataTypeConvert::convertToJson([
            'code' => $code,
            'msg' => $message,
            'status' => $status,
            'list' => $debugArrayInfo
        ], true));
        if (self::$apis == 'restful') {
            self::restfulErrorOutput($code, $message, $status, ($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/' . date('Ymd', time()) . '/' . $debugId));
        }
    }
}
